﻿namespace Hidistro.UI.SaleSystem.Tags
{
    using Hidistro.Core;
    using Hidistro.Core.Enums;
    using Hidistro.Entities;
    using Hidistro.Entities.Commodities;
    using Hidistro.Membership.Context;
    using Hidistro.SaleSystem.Catalog;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Xml;
    /// <summary>
    /// 首页专用，其他页面勿扰，否则后果自负
    /// </summary>
    public class js_Default_SubjectProduct_Simple : WebControl
    {

        private DataTable GetProductList(XmlNode node)
        {
            SubjectListQuery query = new SubjectListQuery
            {
                SortBy = "DisplaySequence",
                SortOrder = SortAction.Desc,
                CategoryIds = node.Attributes["CategoryId"].Value
            };
            if (!string.IsNullOrEmpty(node.Attributes["TagId"].Value))
            {
                query.TagId = int.Parse(node.Attributes["TagId"].Value);
            }
            if (!string.IsNullOrEmpty(node.Attributes["BrandId"].Value))
            {
                query.BrandCategoryId = new int?(int.Parse(node.Attributes["BrandId"].Value));
            }
            if (!string.IsNullOrEmpty(node.Attributes["TypeId"].Value))
            {
                query.ProductTypeId = new int?(int.Parse(node.Attributes["TypeId"].Value));
            }

            string str = node.Attributes["AttributeString"].Value;
            if (!string.IsNullOrEmpty(str))
            {
                IList<AttributeValueInfo> list = new List<AttributeValueInfo>();
                string[] strArray = str.Split(new char[] { ',' });
                for (int i = 0; i < strArray.Length; i++)
                {
                    string[] strArray2 = strArray[i].Split(new char[] { '_' });
                    AttributeValueInfo item = new AttributeValueInfo
                    {
                        AttributeId = Convert.ToInt32(strArray2[0]),
                        ValueId = Convert.ToInt32(strArray2[1])
                    };
                    list.Add(item);
                }
                query.AttributeValues = list;
            }
            query.MaxNum = int.Parse(node.Attributes["MaxNum"].Value);
            return ProductBrowser.GetSubjectList(query);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(this.RendHtml());
        }

        public string RendHtml()
        {
            XmlNode node = TagsHelper.FindProductNode(this.SubjectId, "simple");
            StringBuilder builder = new StringBuilder();
            if (node != null)
            {
                builder.AppendFormat("<div class=\"pro_simple{0} cssEdite \" type=\"simple\" id=\"products_{1}\" >", node.Attributes["ImageSize"].Value, this.SubjectId);
                DataTable productList = this.GetProductList(node);
                if ((productList != null) && (productList.Rows.Count > 0))
                {
                    SiteSettings masterSettings = SettingsManager.GetMasterSettings(false);
                    string str = this.ShowDefaultProductImage(node.Attributes["ImageSize"].Value, masterSettings);
                    builder.AppendLine("<ul>");
                    int s = 1;
                    foreach (DataRow row in productList.Rows)
                    {
                        string str2 = str;
                        if (node.Attributes["ImageSize"].Value != "0")
                        {
                            if (row["ThumbnailUrl" + node.Attributes["ImageSize"].Value] != DBNull.Value)
                            {
                                str2 = row["ThumbnailUrl" + node.Attributes["ImageSize"].Value].ToString();
                            }
                        }
                        else
                        {
                            if (row["ImageUrl6"] != DBNull.Value)
                            {
                                str2 = row["ImageUrl6"].ToString();
                            }
                        }
                        string[] arr;
                        string title = string.Empty;
                        string alt = string.Empty;
                        string keyword = row["Meta_Keywords"].ToString(); ;
                        if (!string.IsNullOrEmpty(keyword))
                        {
                            arr = keyword.Split('|');
                            title = arr[1];
                            alt = arr[2];
                        }


                        builder.AppendLine("<li class=\"floor2-pic pictlong" + s + "\" onmouseout=\"this.className='floor2-pic pictlong" + s + "'\" onmouseover=\"this.className='floor2-pic-two pictlong" + s + "'\">");
                        builder.AppendFormat("<a target=\"_blank\" href=\"{0}\"><img", Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }));
                        if (!string.IsNullOrEmpty(title))
                        {
                            builder.AppendFormat(" title=\"{0}\"", title);
                        }
                        if (!string.IsNullOrEmpty(alt))
                        {
                            builder.AppendFormat(" alt=\"{0}\"", alt);
                        }
                        builder.AppendFormat(" data-original=\"{0}\" /></a>", Globals.ApplicationPath + str2).AppendLine();
                        string str3 = "";
                        if (row["BrandId"] != DBNull.Value)
                        {
                            BrandCategoryInfo t = CategoryBrowser.GetBrandCategory((int)row["BrandId"]);
                            if (t == null)
                            { }
                            else
                                str3 = t.BrandName;
                        }
                        builder.AppendFormat("<div class=\"floor2-pic-box pictlong" + s + "\"><a target=\"_blank\" href=\"{0}\"><div class=\"wnxd_bName\">{2}</div><div class=\"wnxd_pName\">{1}</div></a></div></li>", Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }), row["ProductName"], str3).AppendLine();
                        s++;
                    }
                    builder.AppendLine("</ul>");
                }
                builder.Append("</div>");
            }
            return builder.ToString();
        }

        private string ShowDefaultProductImage(string thumbnailsize, SiteSettings settings)
        {
            string defaultProductImage = settings.DefaultProductImage;
            switch (thumbnailsize)
            {
                case "0":
                    return settings.DefaultProductImage;
                case "40":
                    return settings.DefaultProductThumbnail1;

                case "60":
                    return settings.DefaultProductThumbnail2;

                case "100":
                    return settings.DefaultProductThumbnail3;

                case "160":
                    return settings.DefaultProductThumbnail4;

                case "180":
                    return settings.DefaultProductThumbnail5;

                case "220":
                    return settings.DefaultProductThumbnail6;

                case "310":
                    return settings.DefaultProductThumbnail7;

                case "410":
                    return settings.DefaultProductThumbnail8;
            }
            return defaultProductImage;
        }

        /// <summary>
        /// 配置节点ID
        /// </summary>
        public int SubjectId { get; set; }

    }
}

